﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web.SessionState;

namespace jnlamp.Web.baseinfo
{
    /// <summary>
    /// GetBase 的摘要说明
    /// </summary>
    public class GetBase : IHttpHandler, IRequiresSessionState
    {
        jnlamp.BLL.baseinfo bll = new jnlamp.BLL.baseinfo();
        jnlamp.BLL.mybaseinfo bllmy = new jnlamp.BLL.mybaseinfo();
        StringBuilder result = new StringBuilder();        
        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "querymy")
                Querymy(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "deleteuser")
                deleteuser(context);
            else if (ac == "ddllist")
                ddllist(context);
            else if (ac == "querymyt")
                QueryT(context);
            else if (ac == "queryuser")
                QuerymyT(context);

            else if (ac == "adduser")
                adduser(context);

        }
        public void QuerymyT(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string myuids = "";
                int myuid = 0;                
                if (null != context.Request.Form["uid"])
                {
                    myuids = context.Request.Form["uid"].ToString().Trim();
                    int.TryParse(myuids, out myuid);
                }
                string sql = "uid="+myuid;                
                DataSet ds = bll.GetMyList(sql);
                string str = GetTreeJsonByTableOne(ds.Tables[0], "mbid", "basename");
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void QueryT(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string sebm = "";
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 1000;
                page = 1;
                string sort = "", order = "", orderby = "";
                if (null != context.Request.Form["abh"])
                {
                    sebm = context.Request.Form["abh"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (sebm != "" && sebm != "0")
                {
                    if (sebm.Length == 6)
                    {

                        if (sebm.EndsWith("0000"))
                        {
                            strWhere.AppendFormat(" abh like '{0}%' and ", sebm.Substring(0, 2));
                        }
                        else if (sebm.EndsWith("00"))
                        {
                            strWhere.AppendFormat(" abh like '{0}%' and ", sebm.Substring(0, 4));
                        }
                        else
                        {
                            strWhere.AppendFormat(" abh like '{0}%' and ", sebm);
                        }
                    }
                    else
                    {
                        strWhere.AppendFormat(" abh like '{0}%' and ", sebm);
                    }
                }
                else
                {
                    string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                    string add = string.Join("", pcc);
                    strWhere.AppendFormat(" address like '%{0}%' and ", add);

                }
                //strWhere.AppendFormat(" uid={0} and ", uid);
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    orderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), orderby, (page - 1) * pageRows + 1, page * pageRows);
                string str = GetTreeJsonByTableOne(ds.Tables[0], "bid", "basename");
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        private string GetTreeJsonByTableOne(DataTable tabel, string idCol, string txtCol)
        {
            result.Remove(0, result.Length);
            if (tabel.Rows.Count > 0)
            {
                result.Append("[");

                foreach (DataRow row in tabel.Rows)
                {
                    result.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"address\":\"" + row["address"] + "\"},");
                }
                result = result.Remove(result.Length - 1, 1);

                result.Append("]");

            }
            return result.ToString();
        }
        public void ddllist(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                DataSet ds = bll.GetList("");
                string strJson = ToJson.DataTable2Json1(ds.Tables[0]);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["bid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void deleteuser(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["mbid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bllmy.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int mid = 0;
                string uidstr = context.Request.Form["bid"];
                if (uidstr != "")
                    mid = int.Parse(uidstr);
                string mname = context.Request.Form["basename"];
                string promark = context.Request.Form["basemark"];
                string prodate = context.Request.Form["basedate"];
                string abh = context.Request.Form["abh"];
                string pids = context.Request.Form["pid"];
                int pid = 0;
                int.TryParse(pids, out pid);
                DateTime dt;
                if (prodate != null && prodate != "")
                    DateTime.TryParse(prodate, out dt);
                else
                    dt = DateTime.Now;

                jnlamp.Model.baseinfo model = new jnlamp.Model.baseinfo();
                model.basename = mname;
                model.basemark = promark;
                model.basedate = dt;
                model.pid =pid;
                model.bid = mid;
                model.abh = abh;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string mname = context.Request.Form["basename"];
                string promark = context.Request.Form["basemark"];
                string prodate = context.Request.Form["basedate"];
                string abh = context.Request.Form["abh"];
                string pids=context.Request.Form["pid"];
                int pid = 0;
                int.TryParse(pids, out pid);
                DateTime dt;
                if (prodate != null && prodate != "")
                    DateTime.TryParse(prodate, out dt);
                else
                    dt = DateTime.Now;
                jnlamp.Model.baseinfo model = new jnlamp.Model.baseinfo();
                model.basename = mname;
                model.basemark = promark;
                model.basedate = dt;
                model.abh = abh;
                model.pid = pid;
                if (bll.Add(model) > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void adduser(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                
                string pids = context.Request.QueryString["bid"];
                int bid = 0;
                int.TryParse(pids, out bid);
                string uids = context.Request.QueryString["uid"];
                int muid = 0;
                int.TryParse(uids, out muid);

                jnlamp.Model.mybaseinfo model = new jnlamp.Model.mybaseinfo();
                model.bid = bid;
                model.uid = muid;
               
                if (bllmy.Add(model) > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string uname, utruename, st, sebm = "";
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                if (null != context.Request.Form["sebm"])
                {
                    sebm = context.Request.Form["sebm"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" basename like '%{0}%' and ", uname);
                }
                
                if (sebm != "" && sebm != "0")
                {
                    if (sebm.Length == 6)
                    {

                        if (sebm.EndsWith("0000"))
                        {
                            strWhere.AppendFormat(" abh like '{0}%' and ", sebm.Substring(0, 2));
                        }
                        else if (sebm.EndsWith("00"))
                        {
                            strWhere.AppendFormat(" abh like '{0}%' and ", sebm.Substring(0, 4));
                        }
                        else
                        {
                            strWhere.AppendFormat(" abh like '{0}%' and ", sebm);
                        }
                    }
                    else
                    {
                        strWhere.AppendFormat(" abh like '{0}%' and ", sebm);
                    }
                }
                else
                {
                    string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                    string add = string.Join("", pcc);
                    strWhere.AppendFormat(" address like '%{0}%' and ", add);

                }
                //strWhere.AppendFormat(" uid={0} and ", uid);
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public void Querymy(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            
            if (uid != 0)
            {
                string pname = Web.Admin.myinfo.getPname(context);
                string username= Web.Admin.myinfo.getUsername(context);
                string uname, utruename, st, sebm = "";
                uname = utruename = st = "";
                //获取前台传来的值 
                if (null != context.Request.Form["search_type"])
                {//获取前台传来的值  
                    st = context.Request.Form["search_type"].ToString().Trim();
                }
                if (null != context.Request.Form["search_value"])
                {//获取前台传来的值  
                    uname = context.Request.Form["search_value"].ToString().Trim();
                }
                if (st == "username")
                {
                    utruename = uname;
                    uname = "";
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                if (null != context.Request.Form["sebm"])
                {
                    sebm = context.Request.Form["sebm"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (uname != "")
                {
                    strWhere.AppendFormat(" basename like '%{0}%' and ", uname);
                }
                if (utruename != "")
                {
                    strWhere.AppendFormat(" proname like '%{0}%' and ", utruename);
                }

                if (sebm != "" && sebm != "0")
                {
                    if (sebm.Length == 6)
                    {

                        if (sebm.EndsWith("0000"))
                        {
                            strWhere.AppendFormat(" abh like '{0}%' and ", sebm.Substring(0, 2));
                        }
                        else if (sebm.EndsWith("00"))
                        {
                            strWhere.AppendFormat(" abh like '{0}%' and ", sebm.Substring(0, 4));
                        }
                        else
                        {
                            strWhere.AppendFormat(" abh like '{0}%' and ", sebm);
                        }
                    }
                    else
                    {
                        strWhere.AppendFormat(" abh like '{0}%' and ", sebm);
                    }
                }
                else if (sebm == "")
                {
                    string[] pcc = Web.Admin.myinfo.getPCC(context).Split(',');
                    string add = string.Join("", pcc);
                    strWhere.AppendFormat(" address like '%{0}%' and ", add);
                    if (pname == "技术人员")
                    {
                        BLL.prouserinfo p = new BLL.prouserinfo();
                        string ids = p.GetListPIDs("uid=" + uid);
                        if (ids != "")
                        {
                            int startindex1 = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                            if (startindex1 >= 0)
                            {
                                strWhere.Remove(startindex1, 3);//删除多余的and关键字  
                            }
                            strWhere.AppendFormat(" or pid in({0}) and ", ids);
                        }
                    }

                }
                else {
                    if (pname == "技术人员")
                    {
                        BLL.prouserinfo p = new BLL.prouserinfo();
                        string ids = p.GetListPIDs("uid=" + uid);
                        if (ids != "")
                        {
                            int startindex1 = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                            if (startindex1 >= 0)
                            {
                                strWhere.Remove(startindex1, 3);//删除多余的and关键字  
                            }
                            strWhere.AppendFormat(" or pid in({0}) and ", ids);
                        }
                    }

                }
                int level = Web.Admin.myinfo.getLevel(context);
                if (level == 0)
                {
                    //string username = Web.Admin.myinfo.getUsername(context);
                    strWhere.AppendFormat(" users like '%{0}%' and ", username);
                }
                //strWhere.AppendFormat(" uid={0} and ", uid);
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();

            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}